//
//  HPAbstrictViewController.m
//  ZZTong
//
//  Created by handpay on 13-5-8.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "HPAbstrictViewController.h"
#import "ZZTongUIEngine.h"

#define spaceButtonItemWidth    (([[[UIDevice currentDevice] systemVersion]doubleValue]>=7.0)) ? -7 : 5

@interface HPAbstrictViewController (){
    UIToolbar *inputAccessoryToolbar;
}


// 顶部右侧UIBarButtonItem
@property (nonatomic, retain) UIBarButtonItem *rightBarButtonItem;

@end

@implementation HPAbstrictViewController

@synthesize showBackButton;

@synthesize hideBottomBar;

@synthesize backButton;

@synthesize rightButton;

@synthesize rightBarButtonItem;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hideBottomBar = YES;
        self.showBackButton = YES;
        self.canSave = YES;
    }
    return self;
}


//#define kInputAccessoryViewPrevItemTag      1000001
//
//#define kInputAccessoryViewNextItemTag      1000002
/**
 * 获取输入框的inputAccessoryView
 */
- (UIToolbar *) inputAccessoryView
{
    if (inputAccessoryToolbar == nil) {
        inputAccessoryToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, HPUIENG.scrWidth, 44)];
        [inputAccessoryToolbar setBarStyle:UIBarStyleBlack];
        [inputAccessoryToolbar setTranslucent:YES];
        [inputAccessoryToolbar setBackgroundColor:[UIColor clearColor]];
//        // 上一个
//        UIBarButtonItem *prevItem = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStyleDone target:self action:@selector(prevResponderBecomeFirstResponder)];
//        [prevItem setTag:kInputAccessoryViewPrevItemTag];
//        
//        // 下一个
//        UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStyleDone target:self action:@selector(nextResponderBecomeFirstResponder)];
//        [nextItem setTag:kInputAccessoryViewNextItemTag];
        
        UIBarButtonItem *fixItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyboard)];
        
//        if (![[Utility getSystemVersion] hasPrefix:@"4"]) {
//            prevItem.tintColor = nextItem.tintColor = doneItem.tintColor = [UIColor blackColor];
//        }
//        [inputAccessoryToolbar setItems:[NSArray arrayWithObjects:prevItem, nextItem, fixItem, doneItem, nil]];
//        [prevItem release];
//        [nextItem release];
        [inputAccessoryToolbar setItems:[NSArray arrayWithObjects:fixItem, doneItem, nil]];
        [fixItem release];
        [doneItem release];
    }
    
//    UIBarButtonItem *prevItem = nil;
//    
//    UIBarButtonItem *nextItem = nil;
//    
//    for (UIBarButtonItem *item in inputAccessoryToolbar.items) {
//        if (item.tag == kInputAccessoryViewPrevItemTag) {
//            prevItem = item;
//        }
//        if (item.tag == kInputAccessoryViewNextItemTag) {
//            nextItem = item;
//        }
//    }
//    [prevItem setEnabled:YES];
//    [nextItem setEnabled:YES];
    
//    UIControl *currentResponseView = [Utility currentResponseView:self.view];
//    
//    NSArray *canBecomeFirstResponders = [Utility canBecomeFirstRespondersOrderedInView:self.view];
//    
//    if ([canBecomeFirstResponders objectAtIndex:0] == currentResponseView) {
//        [prevItem setEnabled:NO];
//    }
//    if ([canBecomeFirstResponders lastObject] == currentResponseView) {
//        [nextItem setEnabled:NO];
//    }
    
    return inputAccessoryToolbar;
}

- (void) setButtonBg:(UIButton *)button{
    if (!button) {
        return;
    }
    UIImage *normalImage = [[UIImage imageNamed:@"button_bg.png"]
							stretchableImageWithLeftCapWidth:12.0
							topCapHeight:0.0];
	[button setBackgroundImage:normalImage forState:UIControlStateNormal];
    
    button.exclusiveTouch = YES;
}
- (void)dealloc{
    [inputAccessoryToolbar release];
    
    self.backButton  = nil;
    self.rightButton = nil;
    self.rightBarButtonItem = nil;
    [super dealloc];
}
- (void)hideTabBar:(BOOL)hidden{
//    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0];
        
        int h = ScreenHeight;
        if (HP_Device_iPhone5) {
            h += IPHONE5_ADD_HEIGHT;
        }
//        self.tabBarController.tabBar.hidden = hidden;
        for(UIView *view in self.tabBarController.view.subviews)
        {
            if([view isKindOfClass:[UITabBar class]])
            {
                if (hidden) {
                    [view setFrame:CGRectMake(view.frame.origin.x, h, view.frame.size.width, view.frame.size.height)];
                } else {
                    [view setFrame:CGRectMake(view.frame.origin.x, (h - 49), view.frame.size.width, view.frame.size.height)];
                    
                }
            }
        }
        if (hidden) {
            [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, h)];
        } else {
            [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width,  (h - 49))];
        }
        [UIView commitAnimations];
//    });
    
}

- (void)goBack
{
    [HPUIENG popScreenByNum:1];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self hideTabBar:YES];
}

- (void)viewDidLoad
{
    if ([ZZT_UIENG getCurrentNavigation]) {
        [[ZZT_UIENG getCurrentNavigation] setNavigationBarHidden:NO];
        self.navigationItem.hidesBackButton = YES;
    }

//    [self hideTabBar:YES];
    UIImage *img = [UIImage imageWithColor:kNormalBackgroundColor size:(CGSize){self.view.frame.size.width, self.view.frame.size.height}];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:img];
    
    [bgImageView setFrame:(CGRect){0, 0, self.view.frame.size.width, self.view.frame.size.height}];
    [bgImageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    bgImageView.tag = bgImageViewTag;
    [self.view addSubview:bgImageView];
    [self.view sendSubviewToBack:bgImageView];
    [bgImageView release];

    // 设置返回上一页按钮
    if (self.showBackButton) {
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        [b setExclusiveTouch:YES];
        [b setFrame:CGRectMake(0, 0, 24, 34)];
        [b setContentEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0)];
        [b addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        [b setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        self.backButton = b;
        
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:b];
        UIBarButtonItem *fixedSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        fixedSpaceButtonItem.width = spaceButtonItemWidth;
        self.navigationItem.leftBarButtonItems = @[fixedSpaceButtonItem, leftButton];
        [fixedSpaceButtonItem release];
        //self.navigationItem.leftBarButtonItem = leftButton;
        [leftButton release];
    }
    
    [super viewDidLoad];
    
    if ([ZZT_UIENG getCurrentNavigation]) {
        UILabel *titleLabel = [[UILabel alloc] init];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setText:self.title];
        [titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [titleLabel sizeToFit];
        [self.navigationItem setTitleView:titleLabel];
        [titleLabel setCenter:self.navigationController.navigationBar.center];
        [titleLabel release];
    }
}
//- (void)setNavigationBarTiltle:(NSString *)strTitle
//{
//    CGRect frame = CGRectMake(0, 0, 200, 44);
//    UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
//    label.backgroundColor = [UIColor clearColor];
//    label.font = [UIFont boldSystemFontOfSize:20.0];
//    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
//    label.textAlignment = UITextAlignmentCenter;
//    label.textColor = [UIColor whiteColor];
//    [label setShadowOffset:CGSizeMake(0,1)];
//    label.text = strTitle;
//    self.navigationItem.titleView = label;
//}



- (void) setRightButton:(UIButton *)rightButton_
{
    if (rightButton != rightButton_) {
        [rightButton release];
        rightButton_.exclusiveTouch = YES;
        rightButton = [rightButton_ retain];
        if (self.rightBarButtonItem) {
            if (!rightButton_) {
                self.navigationItem.rightBarButtonItem = nil;
            }else{
                self.rightBarButtonItem.customView = rightButton;
            }
        } else {
//            UIBarButtonItem *rightBarButtonItem_ = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
//            self.rightBarButtonItem = rightBarButtonItem_;
//            [rightBarButtonItem_ release];
//            self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
            
            UIBarButtonItem *rightBarButtonItem_ = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
            self.rightBarButtonItem = rightBarButtonItem_;
            [rightBarButtonItem_ release];
            
            UIBarButtonItem *fixedSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            fixedSpaceButtonItem.width = spaceButtonItemWidth;
            
            self.navigationItem.rightBarButtonItems = @[self.rightBarButtonItem,fixedSpaceButtonItem];
            [fixedSpaceButtonItem release];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
//
//// UINavigationController
//@implementation UINavigationController (Rotation_IOS6)
//
//- (BOOL)shouldAutorotate {
//    return self.topViewController.shouldAutorotate;
//}
//
//- (NSUInteger)supportedInterfaceOrientations {
//    return self.topViewController.supportedInterfaceOrientations;
//}
//
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return [self.topViewController preferredInterfaceOrientationForPresentation];
//}
//@end
//
////
//@implementation UITabBarController (Tabbar_Rotation_IOS6)
//
//- (BOOL)shouldAutorotate {
//    UINavigationController* nav = (UINavigationController*)[self.viewControllers objectAtIndex:self.selectedIndex];
//    return [nav shouldAutorotate];
//}
//
//- (NSUInteger)supportedInterfaceOrientations {
//    UINavigationController* nav = (UINavigationController*)[self.viewControllers objectAtIndex:self.selectedIndex];
//    return [nav supportedInterfaceOrientations];
//}
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    UINavigationController* nav = (UINavigationController*)[self.viewControllers objectAtIndex:self.selectedIndex];
//    return [nav preferredInterfaceOrientationForPresentation];
//}
//@end